package org.algorithm.YaYa.链表;


import org.algorithm.biubiu.wheel_1.common.ListNode;

/*****
 *
 *      周末的题：  周一要看，  没写的， 嘿嘿。。。   你们懂的
 *   参考uri: https://leetcode-cn.com/problems/reverse-nodes-in-k-group/
 *
 */
public class K个一组反转链表 {
    public static void main(String[] args){
        ListNode node = ListNode.build(1,2,3,4,5);
        ListNode res = reverseKGroup(node,3);
        System.out.println(ListNode.print(res));
    }



    public static  ListNode reverseKGroup(ListNode head, int k) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode tail = dummy;
        while (true) {
            int count = 0;
            while (tail != null && count != k) {
                count++;
                tail = tail.next;
            }
            if (tail == null) break;
            ListNode head1 = pre.next;
            while (pre.next != tail) {
                ListNode cur = pre.next;
                pre.next = cur.next;
                cur.next = tail.next;
                tail.next = cur;
            }
            pre = head1;
            tail = head1;
        }
        return dummy.next;
    }

}
